package com.sanpu.kxe.business.card.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseFragment;
import com.sanpu.kxe.business.card.adapter.CreditCardAdapter;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.contract.CreditCardListContract;
import com.sanpu.kxe.business.card.presenter.CreditCardListPresenter;
import com.sanpu.kxe.business.card.provider.CardOpenProvider;
import com.sanpu.kxe.jump.eventbus.RefreshBankEvent;
import com.sanpu.kxe.jump.eventbus.RefreshDefBankEvent;
import com.sanpu.kxe.module.roundview.RoundLinearLayout;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：信用卡列表
 * Created by 薛勇军 on 2019/5/27.
 */

public class CreditCardListFragment extends BaseFragment implements CreditCardListContract.View {

    @Bind(R.id.list_view)
    RecyclerView vRecyclerview;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;

    private RoundLinearLayout addBankRll;

    private LinearLayout vEmptyView;

    private RoundLinearLayout vBindCardRtv;

    private CreditCardAdapter vCreditCardAdapter;

    private CreditCardListContract.Presenter mPresenter;

    private String chkSts = "";

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_reb_total;
    }

    @Override
    protected void initView() {
        super.initView();
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        View headView = LayoutInflater.from(getContext()).inflate(R.layout.view_mybank_list_head, null);
        vEmptyView = ButterKnife.findById(headView, R.id.emptyView);
        vBindCardRtv = ButterKnife.findById(headView, R.id.bindCardRtv);
        View footerView = LayoutInflater.from(getContext()).inflate(R.layout.view_mybank_list_footer, null);
        addBankRll = ButterKnife.findById(footerView, R.id.addBankRll);
        addBankRll.setVisibility(View.GONE);

        vCreditCardAdapter = new CreditCardAdapter();
        vCreditCardAdapter.addHeaderView(headView);
        vCreditCardAdapter.addFooterView(footerView);
        vRecyclerview.setAdapter(vCreditCardAdapter);

        mPresenter = new CreditCardListPresenter(this);
        mPresenter.selectUrmBankPage();
    }

    @Override
    protected void setListener() {
        super.setListener();
        addBankRll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CardOpenProvider.getInstance().openAddCreditNewActivity(getContext());
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.selectUrmBankPage();
            }
        });
        vCreditCardAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                BankInfoOutput infoOutput = (BankInfoOutput) adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.updateTv:
                        mPresenter.updateCardClick(infoOutput);
                        break;
                    case R.id.queryTv:
                        mPresenter.queryCardClick(infoOutput);
                        break;
                    case R.id.refundTv:
                        mPresenter.refundCardClick(infoOutput);
                        break;
                }
            }
        });
        vCreditCardAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BankInfoOutput infoOutput = (BankInfoOutput) adapter.getItem(position);
                if (infoOutput != null) {
                    CardOpenProvider.getInstance().openBankCardDetailsActivity(getContext(), infoOutput, chkSts);
                }
            }
        });
        vBindCardRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CardOpenProvider.getInstance().openAddCreditNewActivity(getContext());
            }
        });
    }

    @Override
    public void showViewData(List<BankInfoOutput> outputs) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        if (outputs != null && outputs.size() > 0) {
            vEmptyView.setVisibility(View.GONE);
            addBankRll.setVisibility(View.VISIBLE);
            vCreditCardAdapter.setNewData(outputs);
        } else {
            vCreditCardAdapter.setNewData(new ArrayList<BankInfoOutput>());
            vEmptyView.setVisibility(View.VISIBLE);
            addBankRll.setVisibility(View.GONE);
        }
        vCreditCardAdapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshCheck(RefreshDefBankEvent event) {
        if (event != null) {
            chkSts = event.getChkSts();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshBank(RefreshBankEvent event) {
        if (event != null) {
            mPresenter.selectUrmBankPage();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(CreditCardListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
